<!DOCTYPE html>

<!-- Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
     for details. All rights reserved. Use of this source code is governed by a
     BSD-style license that can be found in the LICENSE file. -->

<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        body {
            font-size: 12pt;
            margin: 0;
            padding: 0;
        }

        h2 {
            margin-top: 0;
        }
    </style>
    <script>
        replaceJavaScript = function (value) {
            // Remove the old node.
            var oldNode = document.getElementById('compiledJsScript');
            if (oldNode && oldNode.parentNode) {
                oldNode.parentNode.removeChild(oldNode);
            }

            // Create a new node.
            var scriptNode = document.createElement('script');
            scriptNode.setAttribute('id', 'compiledJsScript');
            scriptNode.text = value;
            document.head.appendChild(scriptNode);
        };

        messageHandler = function (e) {
            var obj = e.data;
            var command = obj.command;
            var body = document.body;

            if (command === 'setCss') {
                document.getElementById('styleId').innerHTML = obj.css;
            } else if (command === 'setHtml') {
                body.innerHTML = obj.html;
            } else if (command === 'execute') {
                // Replace all three.
                body.innerHTML = obj.html;
                document.getElementById('styleId').innerHTML = obj.css;
                replaceJavaScript(obj.js);
            }
        };

        window.addEventListener('load', function () {
            window.addEventListener('message', messageHandler, false);
            parent.postMessage({'sender': 'frame', 'type': 'ready'}, '*');
        });
    </script>
    <style id="styleId"></style>
    <script src="require.js"></script>
    <script id="compiledJsScript"></script>
</head>
<body>
</body>
</html>
